package com.xl.xkit.manager

import com.xl.xkit.consts.MK
import com.xl.xkit.consts.getJson
import com.xl.xkit.utils.MKUtil
import org.json.JSONArray

internal class DataCode {
    companion object {
        val instance = SingletonHodler.holder
    }

    private object SingletonHodler {
        val holder = DataCode()
    }


    fun addCpuData(cpuData: Float) {

        val data = getJson(
            "time" to System.currentTimeMillis(),
            "data" to cpuData
        )

        val list: JSONArray = if (getCpu()?.isEmpty() == true) {
            JSONArray()
        } else {
            JSONArray(getCpu())
        }

        list.put(data)

        MKUtil.encode(MK.PERFORMANCE_TYPE_CPU, list.toString())

    }


    fun addMemoryData(memoryData: Float) {
        val data = getJson(
            "time" to System.currentTimeMillis(),
            "data" to memoryData
        )

        val list: JSONArray = if (getMemory()?.isEmpty() == true) {
            JSONArray()
        } else {
            JSONArray(getMemory())
        }

        list.put(data)

        MKUtil.encode(MK.PERFORMANCE_TYPE_MEMORY, data.toString())
    }


    fun addFpsData(fpsData: Float) {

        val data = getJson(
            "time" to System.currentTimeMillis(),
            "data" to fpsData
        )


        val list: JSONArray = if (getFps()?.isEmpty() == true) {
            JSONArray()
        } else {
            JSONArray(getFps())
        }

        list.put(data)

        MKUtil.encode(MK.PERFORMANCE_TYPE_FPS, data.toString())
    }


    fun getCpu(): String? {
        return MKUtil.decodeString(MK.PERFORMANCE_TYPE_CPU)
    }


    fun getMemory(): String? {
        return MKUtil.decodeString(MK.PERFORMANCE_TYPE_MEMORY)
    }


    fun getFps(): String? {
        return MKUtil.decodeString(MK.PERFORMANCE_TYPE_FPS)
    }


}